#!/bin/bash
cd /
image=$1

ucode_level=`rpm -qa | grep "IBMhsc\.ucode\-mgr\-"`
# rpm --erase $ucode_level

SNM_level=`rpm -qa | grep "IBMhsc\.SNM\-"`
# rpm --erase $SNM_level

SNM_GUI_level=`rpm -qa | grep "IBMhsc\.SNM_GUI\-"`
# rpm --erase $SNM_GUI_level

# rpm --install $image/IBMhsc.ucode-mgr-1.1.1.1-1.i386.rpm
# rpm --install $image/IBMhsc.SNM-1.1.1.1-1.i386.rpm
# rpm --install $image/IBMhsc.SNM_GUI-1.1.1.1-1.i386.rpm

rpm -U --force $image/IBMhsc.ucode-mgr-1.1.5.0-1.i386.rpm
rpm -U --force $image/IBMhsc.SNM-1.1.5.0-1.i386.rpm
rpm -U --force $image/IBMhsc.SNM_GUI-1.1.5.0-1.i386.rpm

# For PTF5 and until the base Austin changes are available
#      (hoepfully for PTF6) we need to:
#      Create links in /opt/hsc/bin for commands that used to be in
#           /opt/hsc/bin but are now in /opt/hsc/bin/command
#      Replace fnmAccessMgr.policy and libacmgr.so
#      Move fnm_test from /usr/local/hsctool to
#           /opt/hsc/bin/command
#      Replace hdwr_svr

# Create links that are erased when erasing the old RPM
ln -s /opt/hsc/bin/fnmcommandcontrol /opt/hsc/bin/fnm.snap 2>&1 >> /dev/null
ln -s /opt/hsc/bin/fnmcommandcontrol /opt/hsc/bin/frucode 2>&1 >> /dev/null
ln -s /opt/hsc/bin/fnmcommandcontrol /opt/hsc/bin/instfru 2>&1 >> /dev/null
ln -s /opt/hsc/bin/fnmcommandcontrol /opt/hsc/bin/i_stub_FS 2>&1 >> /dev/null
ln -s /opt/hsc/bin/fnmcommandcontrol /opt/hsc/bin/jt 2>&1 >> /dev/null

# Replace the fnmAccessMgr.policy file
mv /opt/hsc/data/fnmAccessMgr.policy /opt/hsc/data/fnmAccessMgr.policy.prev_to_PTF5
cp -p $image/fnmAccessMgr.policy /opt/hsc/data
chmod 550 /opt/hsc/data/fnmAccessMgr.policy

# Move fnm_test from /usr/local/hsctool to /opt/hsc/bin/command
if [[ -a /usr/local/hsctool/fnm_test ]]
then
	mv /usr/local/hsctool/fnm_test /opt/hsc/bin/command
fi

# Replace the libacmgr.so
mv /usr/lib/libacmgr.so /usr/lib/libacmgr.so.prev_to_PTF5
# Decide if we are on RedHat or MCP
if [[ -a /bin/cut ]]
then
	# We are on RedHat
	cp -p $image/libacmgr.so.r41a /usr/lib/libacmgr.so
else
	# We are on MCP
	cp -p $image/libacmgr.so.r42 /usr/lib/libacmgr.so
fi
chmod 555 /usr/lib/libacmgr.so

# To replace hardware server, need to mess with /etc/inittab
# First remove the entry from /etc/inittab
/bin/sed -e '/^hsv:/d' /etc/inittab > /tmp/hsc_tmp_inittab

# Now add the commented entry and mv the file back
echo "#hsv:2345:respawn:/opt/hsc/bin/startHDWR_SVR 1>/dev/null 2>&1" >> /tmp/hsc_tmp_inittab
mv /tmp/hsc_tmp_inittab /etc/inittab

# Cause the file to be re-examined
/sbin/telinit q

# Now copy over the new hdwr_svr executable after saving the old
mv /opt/hsc/bin/hdwr_svr /opt/hsc/bin/hdwr_svr.prev_to_PTF5
if [[ -a /bin/cut ]]
then
	# We are on RedHat
	cp -p $image/hdwr_svr.r41a /opt/hsc/bin/hdwr_svr
else
	# We are on MCP
	cp -p $image/hdwr_svr.r42 /opt/hsc/bin/hdwr_svr
fi
chmod 555 /opt/hsc/bin/hdwr_svr

# Now fix up /etc/inittab
# First remove the entry from /etc/inittab
/bin/sed -e '/^#hsv:/d' /etc/inittab > /tmp/hsc_tmp_inittab

# Now add the uncommented entry and mv the file back
echo "hsv:2345:respawn:/opt/hsc/bin/startHDWR_SVR 1>/dev/null 2>&1" >> /tmp/hsc_tmp_inittab
mv /tmp/hsc_tmp_inittab /etc/inittab

# Do not need to run telinit q because a reboot will follow this install

exit 0
